addpath(genpath('C:\Users\BCoe\matlab_code\new_magnet_tasks'))
clear
clear java
clear global
clear persistent
java.lang.System.gc()
java.lang.Runtime.getRuntime.gc
c=define_colors(uint8(256));

KbQueueCreate();
KbQueueStart;

totalMemory=java.lang.Runtime.getRuntime.totalMemory;
freeMemory=java.lang.Runtime.getRuntime.freeMemory;
if freeMemory/totalMemory<.05
    fprintf('there is only %3.2f%% of free JAVA memory left\n -> exiting... please restart matlab\n',freeMemory/totalMemory*100);
    return
else
    fprintf('there is %3.2f%% of free JAVA memory left\n',freeMemory/totalMemory*100);
end
abs_startUS=round(getsecs*10000);
Memorytime=zeros(1,10000);
[correct, ~, alphac] = imread('Action-ok-icon.png');
[error  , ~, alphae] = imread('Delete-icon.png');
correct(:,:,4) = alphac(:,:);
error(:,:,4) = alphae(:,:);
clear alphac alphae

%% screen
try
    oldLevel = Screen('Preference', 'VisualDebugLevel', 1); %level 1: error only
    oldEnableFlag = Screen('Preference', 'SuppressAllWarnings', 1);
    Screen('Preference', 'SkipSyncTests', 1);
    
    [w, wRect]=Screen('OpenWindow', 2,0);
    screenpixW=wRect(RectRight);
    screenpixH=wRect(RectBottom);
    c_x=round(double(screenpixW/2)); % the stupid text commands require a double for pixels placement
    c_y=round(double(screenpixH/2));
    Screen('TextStyle',w,1);% 0=plain, 1=bold, 2=italics, 3=1&2,  4=underlined, 5=4&1,6=4&2, 7=4&3. rem(?,8)
    Screen('TextFont',w,'Tahoma');
    Screen('TextSize',w,40);
    centertextY=c_y-40*.8;
    
    %[w, wRect]=Screen('OpenWindow', screennum, 0,[],32,2);
    Screen(w,'BlendFunction',GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    OK=Screen('MakeTexture',w,correct);
    NG=Screen('MakeTexture',w,error);
    clear correct error
    r_time=0;
            count=fprintf('running daf;lk gl/k fg;kljharfdl ;kjadflkj hgearjhlgar jkha\n');
    event_time=round(getsecs*10000)-abs_startUS; % update only once per loop

    for ii =1:10000
        if floor(Memorytime(ii))>=r_time+10 
            r_time=event_time;
            %clc; % holy noodles does this eat memory!!!!
            %home
            fprintf(repmat('\b',1,count))
            count=fprintf('running daf;lk gl/k fg;kljharfdl ;kjadflkj hgearjhlgar jkha\n');          
        end
        %home;
        %[keyPress, keyCodes, keyTimes]= Kb_bcoeMS(abs_startUS/10);
        getmouse;
        %DrawFormattedText(w,sprintf('testies:%d',ii),'center',centertextY,c.ORANGE);
        if rem(ii,2)==0
            Screen('DrawTexture',w,OK);% center by default
        else
            Screen('DrawTexture',w,NG);% center by default
        end
        Screen('flip',w,[],[],2);
        
        Memorytime(ii)=round(getsecs*10000)-abs_startUS;
        freeMemory(ii)=java.lang.Runtime.getRuntime.freeMemory;

    end
    fprintf('it worked\n\n')
catch badness
    badness.message
    badness.stack
end

Screen('Closeall');
KbQueueStop;
KbQueueRelease;
%int_hist(round(freeMemory/1024)/(totalMemory/1024)*1000);
figure(1);
clf; hold on
plot(freeMemory/totalMemory*100,'b')
%plot([1 diff(Memorytime)]/mean(diff(Memorytime))*10,'r')
if mean(diff(Memorytime))<50 
    plot([1 diff(Memorytime)],'r')
    ylabel('micro seconds')
else
    plot([1 diff(Memorytime)]/10,'r')
    ylabel('milli seconds')
end
plot(freeMemory/totalMemory*100,'b')
ylim([0 100])